﻿<template>
    <div v-loading="loadingState.configLoading||loadingState.fieldLoading||loadingState.unitLoading" style="padding-top: 20px;">

      <el-form :model="existsCompareConfig">

        <el-form-item label="成本拉齐依据"
                      :label-width="formLabelWidth">
          <template slot="label">成本拉齐依据
            <el-tooltip content="现金流引用" >
              <i class="el-icon-question"/>
            </el-tooltip>
          </template>
          <el-cascader :props="{emitPath: false}"
                       :options="yearRelatedComputeUnitList"
                       filterable
                       v-model="existsCompareConfig.cashFlowRef" :disabled="!modelEditable"></el-cascader>
        </el-form-item>

        <el-form-item label="成本拉齐指标"
                      :label-width="formLabelWidth">
          <template slot="label">成本拉齐指标
            <el-tooltip content="通过对选中的指标进行按百分比降低折算，达到现金流为0" >
              <i class="el-icon-question"/>
            </el-tooltip>
          </template>
          <el-cascader :props="recordFieldCascaderProps"
                       :options="inputTableList"
                       filterable
                       v-model="existsCompareConfig.costFieldList" :disabled="!modelEditable"></el-cascader>
        </el-form-item>
        <el-form-item label="对减指标"
                      :label-width="formLabelWidth">
          <template slot="label">对减指标
            <el-tooltip content="选中的指标可直接进行有项目无项目数据相减" >
              <i class="el-icon-question"/>
            </el-tooltip>
          </template>
          <el-cascader :props="{emitPath: false,multiple:true}"
                       :options="computeUnitList"
                       filterable
                       v-model="existsCompareConfig.subtractRefList" :disabled="!modelEditable"></el-cascader>
        </el-form-item>

          <el-form-item label="特殊处理指标"
                        :label-width="formLabelWidth">
            <template slot="label">特殊处理指标
              <el-tooltip content="选中的指标需要在对减指标对减完成后进行重新计算" >
                <i class="el-icon-question"/>
              </el-tooltip>
            </template>
            <el-cascader :props="{emitPath: false,multiple:true}"
                         :options="computeUnitList"
                         filterable
                         v-model="existsCompareConfig.recalculateRefList"
                         :disabled="!modelEditable"></el-cascader>
          </el-form-item>
          <el-form-item label="无项目风险分析变化因素"
                        :label-width="formLabelWidth">
            <template slot="label">无项目风险分析变化因素
              <el-tooltip content="敏感性分析、平衡性分析中只针对选中的风险因素对无项目数据进行修改" >
                <i class="el-icon-question"/>
              </el-tooltip>
            </template>
            <data-select multiple="true"
                         :data-url="'/riskAnalysis/item/data?modelId='+modelInfo.id"
                         filterable
                         value-key="code"
                         v-model="existsCompareConfig.noProjectRiskItemList"
                         :disabled="!modelEditable"></data-select>
          </el-form-item>
          <el-form-item label=""
                        :label-width="formLabelWidth">

          <el-button v-if="modelEditable" type="primary" @click="updateConfig">保 存</el-button>
          </el-form-item>
      </el-form>
    </div>

</template>

<script>
    import MetaSelect from "../../components/meta-select";
    export default {
        name: "ExistsCompareModule",
      components: {MetaSelect},
      $$route: {
            path: 'exists-compare',
            parentName: 'ModelInfo',
        },
        props:{
            modelInfo:{}
        },
        data() {
            return {
              loadingState:{
                fieldLoading:false,
                unitLoading:false,
                configLoading:false,
              },

                formLabelWidth: '120px',
                compareItem: {fieldIdList: []},
                itemList: [],
                itemDlgVisible: false,
                editItemMode: false,
                inputTableList: [],
                modelEditable: false,
                recordFieldCascaderProps: {
                    label: 'name',
                    value: 'id',
                    emitPath: false,
                    children: 'fieldList',
                    multiple: true,
                },

                configDlgVisible: false,
                yearRelatedComputeUnitList: [],
                computeUnitList: [],
                existsCompareConfig: { computeUnitRefList: []},
                showSheetIcon: false,
            }
        },
        computed: {
            showSelectIconProp: function () {
                if (this.configDlgVisible == false) {
                    this.showSheetIcon = false;
                }
                return this.configDlgVisible && this.showSheetIcon;
            }
        },
        created: function () {
            // const modelId = this.$route.params.id;
            const modelId = this.modelInfo.id;
            // this.loadModelInfo(modelId);
            this.modelEditable = this.modelInfo.state != 'publish';
            this.loadInputTableList(this.modelInfo.id);
            this.loadComputeUnitList(modelId);
            this.loadConfig(modelId);
        },
        methods: {
            // loadModelInfo: function (modelId) {
            //     this.$get("/model/" + modelId, {}, (response) => {
            //         this.modelInfo = response.data;
            //         this.modelEditable = this.modelInfo.state == 0;
            //         this.loadInputTableList(this.modelInfo.id);
            //     });
            // },
            loadInputTableList: function (modelId) {
                this.loadingState.fieldLoading = true;
                this.$get("/table/fulldata?modelId=" + modelId, {}, (response) => {
                    this.inputTableList = response.data;
                }, () => {
                    this.loadingState.fieldLoading = false;
                });
            },

            openCreateItmeDlg: function () {
                this.itemDlgVisible = true;
                if (this.editItemMode) {
                    this.compareItem = {};
                    this.editItemMode = false;
                }

            },

          parseUnitList2OptionList(dataList) {
            var dataMap = {};
            for (var i = 0; i < dataList.length; i++) {
              var item = dataList[i];
              // this.computeUnitMap[item.ref] = item;
              var list = dataMap[item.tag];
              if (!list) {
                list = [];
                dataMap[item.tag] = list;
              }
              list.push(item);
            }
            var resultList = [];
            for (var key in dataMap) {
              var list = dataMap[key];
              var item = {label: key, value: key, children: []};
              for (var i = 0; i < list.length; i++) {
                item.children.push({label: list[i].name, value: list[i].ref});
              }
              resultList.push(item);
            }
            return resultList;
          },
          loadComputeUnitList: function (id) {
            this.loadingState.unitLoading = true;
            this.$get("/compute/data", {
              modelId: id, type: 'field,input,expression,compute,summary'
            }, (response) => {

              var normalList = [];
              for (var item of response.data) {
                if (item.type == 'compute') {
                } else {
                  normalList.push(item);
                }

              }


              this.computeUnitList = this.parseUnitList2OptionList(response.data);
              this.yearRelatedComputeUnitList = this.parseUnitList2OptionList(normalList);

            },()=>{
              this.loadingState.unitLoading = false;
            });

          },

          updateConfig: function () {

            this.loadingState.configLoading = true;
            this.existsCompareConfig.modelId = this.modelInfo.id;
            this.$post("/exists_compare/config/update", this.existsCompareConfig, (response) => {
              this.$message({
                message: '修改成功',
                type: 'success'
              });
              this.configDlgVisible = false;
            }, () => {
              this.loadingState.configLoading = false;
            });
          },
          loadConfig: function (modelId) {
            this.loadingState.configLoading = true;

            this.$get("/exists_compare/config/data", {modelId: modelId}, (response) => {
              if (response.data) {
                this.existsCompareConfig = response.data;
              }
            }, () => {
              this.loadingState.configLoading = false;
            });
          },

        },

    }
</script>

